package com.ny.zmb.glxt.controller;

import com.baomidou.mybatisplus.generator.FastAutoGenerator;
import com.baomidou.mybatisplus.generator.config.OutputFile;
import com.baomidou.mybatisplus.generator.config.rules.DbColumnType;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.sql.Types;
import java.util.Collections;

/**
 *
 * 代码生成器
 *
 * createAuthor : Xmw
 * createTime : 2024-10-10 15:19
 */
@RestController
@CrossOrigin
public class CeshiController {

    @RequestMapping("/ceshi156")
    public String ceshi156(){
        return "ceshi156";
    }

    public static void main(String[] args) {
        FastAutoGenerator.create("jdbc:mysql://rm-wz95m2h0c22x9i431do.mysql.rds.aliyuncs.com/pm_zhaimobao_com?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8",
                        "pm_zhaimobao_com",
                        "GsGP6t2d5T73ZYDr")
                .globalConfig(builder -> {
                    builder.author("xie") // 设置作者
                            .enableSwagger() // 开启 swagger 模式
                            .outputDir("L:\\Java\\Web\\openapi-demo\\src\\main\\java"); // 指定输出目录
                })
                .dataSourceConfig(builder ->
                        builder.typeConvertHandler((globalConfig, typeRegistry, metaInfo) -> {
                            int typeCode = metaInfo.getJdbcType().TYPE_CODE;
                            if (typeCode == Types.SMALLINT) {
                                // 自定义类型转换
                                return DbColumnType.INTEGER;
                            }
                            return typeRegistry.getColumnType(metaInfo);
                        })
                )
                .packageConfig(builder ->
                        builder.parent("com.ny.zmb") // 设置父包名
                                .moduleName("glxt") // 设置父包模块名
                                .pathInfo(Collections.singletonMap(OutputFile.xml, "L:\\Java\\Web\\openapi-demo\\src\\main\\java\\com\\ny\\zmb\\glxt\\mapper")) // 设置mapperXml生成路径
                )
                .strategyConfig(builder ->
                        builder.addInclude("pm_store") // 设置需要生成的表名
//                                .addTablePrefix("t_", "c_") // 设置过滤表前缀
                )
                .templateEngine(new FreemarkerTemplateEngine()) // 使用Freemarker引擎模板，默认的是Velocity引擎模板
                .execute();
    }


}
